package com.hnchances.controller;


import com.hnchances.config.JwtConfig;
import com.hnchances.entity.Grades;
import com.hnchances.entity.User;
import com.hnchances.service.GradesService;
import com.hnchances.service.UserService;
import com.hnchances.vo.Result;
import com.hnchances.vo.SearchVo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author xyp
 *
 */
@RestController
@RequestMapping("/grades")
public class GradesController {
    @Resource
    private GradesService gradesService;
    @Resource
    private JwtConfig jwtConfig;
    @Resource
    private UserService userService;

    /**
     * 成绩添加
     * 地址：localhost:8000/grades/add
     * 请求头 Authorization:eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzUxMiJ9.eyJzdWIiOiIyIiwiaWF0IjoxNjYwMDEwMzk1LCJleHAiOjE2NjAwMTM5OTV9.WTPRJ_ibYgQkLt88gluYtKMkPB1AxHGdiyETADIfEVgC4rTsYLf2DxLoN-o3IUPvy4NDREt56AQZbNAFklgQ9g
     * 参数：
     * {
     *     "courseid":3,
     *     "examid":2,
     *     "classid":1,
     *     "grades":80,
     *     "sid":1
     * }
     * @param grades
     * @return
     */
    @PostMapping("/add")
    public Result add(@RequestBody Grades grades, HttpServletRequest request){
        //获取当前登录人的信息，判断是否为教师权限
        String token = request.getHeader("Authorization");
        String loginId = jwtConfig.getUsernameFromToken(token);
        User dbUser = userService.getById(loginId);
        if (dbUser.getIdentity()!=1){
            return Result.error().message("您无权添加学生成绩");
        }
        return gradesService.add(grades,dbUser);
    }


    /**
     * 成绩修改
     * 地址：localhost:8000/grades/update
     * 请求头 Authorization:eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzUxMiJ9.eyJzdWIiOiIyIiwiaWF0IjoxNjYwMDEwMzk1LCJleHAiOjE2NjAwMTM5OTV9.WTPRJ_ibYgQkLt88gluYtKMkPB1AxHGdiyETADIfEVgC4rTsYLf2DxLoN-o3IUPvy4NDREt56AQZbNAFklgQ9g
     * 参数：
     * {
     *     "id":2,
     *     "grades":100
     * }
     *
     * @param grades
     * @param request
     * @return
     */
    @PutMapping("/update")
    public Result update(@RequestBody Grades grades, HttpServletRequest request){
        //获取当前登录人的信息，判断是否为教师权限
        String token = request.getHeader("Authorization");
        String loginId = jwtConfig.getUsernameFromToken(token);
        User dbUser = userService.getById(loginId);
        if (dbUser.getIdentity()!=1){
            return Result.error().message("您无权修改学生成绩");
        }
        return gradesService.update(grades,dbUser);
    }

    /**
     * 删除用户
     * 地址：localhost:8000/grades/1
     * 请求头：Authorization:eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzUxMiJ9.eyJzdWIiOiIxIiwiaWF0IjoxNjU5OTQ5NzE2LCJleHAiOjE2NTk5NTMzMTZ9.tQvTfq31wxd-seq95pIGCxiXp4sqcFahHMgM9LH3bPxMLWAIRCO5kdExNROEy9sYTID7EnUw9PnzB8XbfkvHtQ
     * @param id
     * @param request
     * @return
     */
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable("id") String id, HttpServletRequest request){
        String token = request.getHeader("Authorization");
        String loginId = jwtConfig.getUsernameFromToken(token);
        User dbUser = userService.getById(loginId);
        if (dbUser.getIdentity()==0){
            return Result.error().message("您无权限删除信息");
        }else {
            return gradesService.delete(id);
        }
    }

    /**
     * 查询
     * 地址：localhost:8000/grades/search
     * 请求头：Authorization:eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzUxMiJ9.eyJzdWIiOiIxIiwiaWF0IjoxNjU5OTQ5NzE2LCJleHAiOjE2NTk5NTMzMTZ9.tQvTfq31wxd-seq95pIGCxiXp4sqcFahHMgM9LH3bPxMLWAIRCO5kdExNROEy9sYTID7EnUw9PnzB8XbfkvHtQ
     * 参数：
     * {
     *     "pageNumber":1,
     *     "pageCount":2,
     *     "stuname":"张三"
     * }
     * @param searchVo
     * @return
     */
    @PostMapping("/search")
    public Result search(@RequestBody SearchVo searchVo, HttpServletRequest request){
        String token = request.getHeader("Authorization");
        String loginId = jwtConfig.getUsernameFromToken(token);
        User dbUser = userService.getById(loginId);
        if (dbUser.getIdentity()==0){
            return Result.error().message("您无权限删除信息");
        }
        return gradesService.search(searchVo);
    }
}

